﻿using ChiWalker.Dynamics;
using ChiWalker.Helpers;
using ChiWalker.Settings;
using ChiWalker.Spell;
using ChiWalker.Utilities;
using Styx;
using Styx.TreeSharp;

namespace ChiWalker.Routines.Monk
{
    internal class Windwalker
    {
        [Behavior(BehaviorType.Pull | BehaviorType.Combat)]
        [Spec(WoWSpec.MonkWindwalker)]
        [Class(WoWClass.Monk)]
        public static Composite WindwalkerMonk()
        {
            return
                new PrioritySelector(
                    new CastOnSelf(Spell.ExpelHarm, ret => StyxWoW.Me.HealthPercent <= 75),
                    Safers.EnsureTarget(),
                    new CastOnSelf(Spell.TigersLust, ret => StyxWoW.Me.IsImmobilized()),
                    new CastOnSelf(Spell.ChiBrew, ret => StyxWoW.Me.CurrentChi < 4),
                    Movement.MoveToLoS(ret => StyxWoW.Me.CurrentTarget),
                    Movement.FaceMyTarget(),
                    new CastOnSelf(Spell.TouchofKarma, ret => StyxWoW.Me.CurrentTarget.CurrentTarget == StyxWoW.Me),
                    new CastOnUnit(Spell.SpinningFireBlossom, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            StyxWoW.Me.CurrentTarget.Distance > 10f + StyxWoW.Me.CurrentTarget.InteractRange &&
                            StyxWoW.Me.IsSafelyFacing(StyxWoW.Me.CurrentTarget, 25f)),
                    new CastOnUnit(Spell.FlyingSerpentKick, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            ChiWalkerSettings.Instance.Monk.UseFlyingSerpentKick &&
                            !ChiWalkerSettings.Instance.DisableMovement &&
                            StyxWoW.Me.CurrentTarget.Distance > 15f &&
                            StyxWoW.Me.IsSafelyFacing(StyxWoW.Me.CurrentTarget, 25f)),
                    Movement.MoveToUnit(ret => StyxWoW.Me.CurrentTarget),
                    new CastOnMyTarget(Spell.InvokeXuen,
                        ret =>
                            SettingsAnalyizer.AnalizeSpellUsage(ChiWalkerSettings.Instance.Monk.InvokeXuen)),
                    new CastOnUnit(Spell.SpearHandStrike, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            StyxWoW.Me.CurrentTarget.CanInterruptCurrentSpellCast &&
                            StyxWoW.Me.CurrentTarget.IsCasting),
                    new CastOnSelf(Spell.EnergizingBrew, ret => StyxWoW.Me.CurrentEnergy <= 40),
                    new CastOnMyTarget(Spell.ChargingOxWave, ret =>
                        Clusters.GetClusterCount(StyxWoW.Me,
                            Clusters.NearbyUnfriendlyUnits,
                            ClusterType.Cone,
                            8f) >= 2),
                    new CastOnSelf(Spell.LegSweep,
                        ret =>
                            Clusters.GetClusterCount(StyxWoW.Me, Clusters.NearbyUnitsInCombatWithMe,
                                ClusterType.Radius, 6f) > 2),
                    new CastOnSelf(Spell.TigereyeBrew,
                        ret =>
                            StyxWoW.Me.HasAura("Tigereye Brew") &&
                            StyxWoW.Me.GetAuraByName("Tigereye Brew").StackCount == 10),
                    new CastOnUnit(Spell.TouchOfDeath, ret => StyxWoW.Me.CurrentTarget,
                        ret =>
                            StyxWoW.Me.HasAura("Death Note") && StyxWoW.Me.CurrentTarget.HealthPercent > 10 &&
                            StyxWoW.Me.CurrentTarget.HealthPercent < StyxWoW.Me.HealthPercent),
                    new CastOnUnit(Spell.RisingSunKick, ret => StyxWoW.Me.CurrentTarget),
                    new CastOnUnit(Spell.FistsofFury, ret => StyxWoW.Me.CurrentTarget),
                    new CastOnMyTarget(Spell.SpinningCraneKick,
                        ret =>
                            Clusters.GetClusterCount(StyxWoW.Me, Clusters.NearbyUnitsInCombatWithMe,
                                ClusterType.Radius, 8f) > 2),
                    new CastOnUnit(Spell.TigerPalm, ret => StyxWoW.Me.CurrentTarget,
                        ret => StyxWoW.Me.GetAuraTimeLeft("Tiger Power", true).TotalSeconds <= 3),
                    new CastOnUnit(Spell.BlackoutKick, ret => StyxWoW.Me.CurrentTarget, ret => StyxWoW.Me.CurrentChi > 2),
                    new CastOnUnit(Spell.Jab, ret => StyxWoW.Me.CurrentTarget)
                    );
        }

        [Behavior(BehaviorType.PreCombatBuffs)]
        [Spec(WoWSpec.MonkWindwalker)]
        [Class(WoWClass.Monk)]
        public static Composite WindwalkerMonkBuff()
        {
            return
                new Decorator(ret => !StyxWoW.Me.Mounted,
                    new PrioritySelector(
                        Group.BuffKings(),
                        Group.BuffCritChance(),
                        new CastOnSelf(Spell.StanceoftheFierceTiger,
                            ret => !StyxWoW.Me.HasAura("Stance of the Fierce Tiger"))
                        ));
        }
    }
}